#ifndef CENTRY_H
#define CENTRY_H

#include <QString>
#include <QVariant>

#include <QtCrypto>

#include "src/ctreeitem.h"
#include "src/cdatabase.h"

struct CEntryField
{
private:
    QVariant m_value;
    QByteArray m_name;
    QString m_md5;

public:
    enum
    {
        Text = 0,
        Password,
        Url,
        Image,
        File
    };

    QString name();
    int type;
    int order;
    QString id;
    QVariant value();
    void setName(const QByteArray&);
    void setValue(const QVariant&);
    QString md5();

    bool operator<(CEntryField& e) { return order<e.order; }
};

struct CEntryFieldPointer
{
    CEntryField *e;
    CEntryFieldPointer(CEntryField *entry): e(entry) {}

    bool operator<(const CEntryFieldPointer& ep) const { return e->order<ep.e->order; }
};

class CEntry: public QObject
{
    Q_OBJECT

    CDatabase &m_db;
    QString m_id;
    QMap<QString, QVariant> m_data;

    QList<CEntryField*> m_fields;

    bool fill();
    void debug(QString msg) const { qDebug() << "[Entry]\t" + msg; }
    int receiveFields();

public:
    CEntry(CDatabase &db, QString id);
    ~CEntry();

    QVariant getData(int col, bool show=true);
    QVariant getDecoration(int col);

    QList<CEntryField*> getFields();
    CEntryField* getField(int id);
    int fieldsCount();
    static int columnCount(bool all=false);

    QString id() { return m_id; }

    static QString getFieldName(int id);
    static QString getStringById(int id);

    bool haveCustomIcon();

    enum EAttribute
    {
        Name = 0,
        Comments,
        Login,
        Password,
        Creation,
//        Expiration,
        Parent,
//        Image,
        Icon,
        IconId,
        Access,
        Modifyed,
        Id,

        END_OF_TYPES
    };
};

#endif // CENTRY_H
